{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "[{'faceId': '8288c256-145c-419a-b9dc-544bb5ad92c7', 'faceRectangle': {'top': 145, 'left': 101, 'width': 200, 'height': 200}, 'faceAttributes': {'smile': 0.0, 'headPose': {'pitch': -9.3, 'roll': -2.6, 'yaw': -3.4}, 'gender': 'male', 'age': 21.0, 'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1}, 'glasses': 'NoGlasses', 'emotion': {'anger': 0.0, 'contempt': 0.0, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.0, 'neutral': 1.0, 'sadness': 0.0, 'surprise': 0.0}, 'blur': {'blurLevel': 'medium', 'value': 0.25}, 'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58}, 'noise': {'noiseLevel': 'low', 'value': 0.22}, 'makeup': {'eyeMakeup': False, 'lipMakeup': False}, 'hair': {'bald': 0.17, 'invisible': False, 'hairColor': [{'color': 'black', 'confidence': 1.0}, {'color': 'other', 'confidence': 0.85}, {'color': 'gray', 'confidence': 0.39}, {'color': 'brown', 'confidence': 0.19}, {'color': 'red', 'confidence': 0.05}, {'color': 'blond', 'confidence': 0.02}, {'color': 'white', 'confidence': 0.0}]}}}]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "b'[{\"faceId\":\"8288c256-145c-419a-b9dc-544bb5ad92c7\",\"faceRectangle\":{\"top\":145,\"left\":101,\"width\":200,\"height\":200},\"faceAttributes\":{\"smile\":0.0,\"headPose\":{\"pitch\":-9.3,\"roll\":-2.6,\"yaw\":-3.4},\"gender\":\"male\",\"age\":21.0,\"facialHair\":{\"moustache\":0.1,\"beard\":0.1,\"sideburns\":0.1},\"glasses\":\"NoGlasses\",\"emotion\":{\"anger\":0.0,\"contempt\":0.0,\"disgust\":0.0,\"fear\":0.0,\"happiness\":0.0,\"neutral\":1.0,\"sadness\":0.0,\"surprise\":0.0},\"blur\":{\"blurLevel\":\"medium\",\"value\":0.25},\"exposure\":{\"exposureLevel\":\"goodExposure\",\"value\":0.58},\"noise\":{\"noiseLevel\":\"low\",\"value\":0.22},\"makeup\":{\"eyeMakeup\":false,\"lipMakeup\":false},\"hair\":{\"bald\":0.17,\"invisible\":false,\"hairColor\":[{\"color\":\"black\",\"confidence\":1.0},{\"color\":\"other\",\"confidence\":0.85},{\"color\":\"gray\",\"confidence\":0.39},{\"color\":\"brown\",\"confidence\":0.19},{\"color\":\"red\",\"confidence\":0.05},{\"color\":\"blond\",\"confidence\":0.02},{\"color\":\"white\",\"confidence\":0.0}]}}}]'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 先导入为们需要的模块\n",
    "import requests\n",
    "import json\n",
    "\n",
    "KEY = '7584f022831e438bb7971ab31f094861'  # Replace with a valid Subscription Key here.\n",
    "# Base URL,  Request URL中 符号?以前\n",
    "#。                          eastasia.api.cognitive.microsoft.com  ==》{endpoint}\n",
    "BASE_URL = 'https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/detect' # 人脸检测\n",
    "\n",
    "# 沿用API的示范代碼，{subscription key}用KEY代入\n",
    "HEADERS = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), #''\n",
    "}\n",
    "\n",
    "img_url = 'http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg'\n",
    "\n",
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,smile,facialHair,emotion,makeup,headPose,hair,blur,exposure,noise'),\n",
    "}\n",
    "#\n",
    "# o = requests.post(BASE_URL,data=json.dumps(data),params = payload,headers=HEADERS)\n",
    "#\n",
    "# print(o.status_code)\n",
    "#\n",
    "# print(o.content)\n",
    "#\n",
    "# print(type(o.json())) # 将json转化成python中的数据结构\n",
    "# o.json()\n",
    "\n",
    "# 坑。参考http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  【更加复杂的post请求】\n",
    "# 差別是 string 字串 vs. dict 字典\n",
    "# Azura 使用的是 data = json.dumps(payload) 或 json=payload，data = payload 会出错\n",
    "r = requests.post(BASE_URL, data=json.dumps(data), params=payload, headers=HEADERS)\n",
    "\n",
    "r.status_code\n",
    "print(r.status_code)\n",
    "r.json()\n",
    "print(r.json())\n",
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '8288c256-145c-419a-b9dc-544bb5ad92c7',\n",
       "  'faceRectangle': {'top': 145, 'left': 101, 'width': 200, 'height': 200},\n",
       "  'faceAttributes': {'smile': 0.0,\n",
       "   'headPose': {'pitch': -9.3, 'roll': -2.6, 'yaw': -3.4},\n",
       "   'gender': 'male',\n",
       "   'age': 21.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.0,\n",
       "    'neutral': 1.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'medium', 'value': 0.25},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.58},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.22},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'hair': {'bald': 0.17,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 1.0},\n",
       "     {'color': 'other', 'confidence': 0.85},\n",
       "     {'color': 'gray', 'confidence': 0.39},\n",
       "     {'color': 'brown', 'confidence': 0.19},\n",
       "     {'color': 'red', 'confidence': 0.05},\n",
       "     {'color': 'blond', 'confidence': 0.02},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.smile</th>\n",
       "      <th>faceAttributes.headPose.pitch</th>\n",
       "      <th>faceAttributes.headPose.roll</th>\n",
       "      <th>faceAttributes.headPose.yaw</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>...</th>\n",
       "      <th>faceAttributes.blur.value</th>\n",
       "      <th>faceAttributes.exposure.exposureLevel</th>\n",
       "      <th>faceAttributes.exposure.value</th>\n",
       "      <th>faceAttributes.noise.noiseLevel</th>\n",
       "      <th>faceAttributes.noise.value</th>\n",
       "      <th>faceAttributes.makeup.eyeMakeup</th>\n",
       "      <th>faceAttributes.makeup.lipMakeup</th>\n",
       "      <th>faceAttributes.hair.bald</th>\n",
       "      <th>faceAttributes.hair.invisible</th>\n",
       "      <th>faceAttributes.hair.hairColor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8288c256-145c-419a-b9dc-544bb5ad92c7</td>\n",
       "      <td>145</td>\n",
       "      <td>101</td>\n",
       "      <td>200</td>\n",
       "      <td>200</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-9.3</td>\n",
       "      <td>-2.6</td>\n",
       "      <td>-3.4</td>\n",
       "      <td>male</td>\n",
       "      <td>...</td>\n",
       "      <td>0.25</td>\n",
       "      <td>goodExposure</td>\n",
       "      <td>0.58</td>\n",
       "      <td>low</td>\n",
       "      <td>0.22</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0.17</td>\n",
       "      <td>False</td>\n",
       "      <td>[{'color': 'black', 'confidence': 1.0}, {'colo...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  8288c256-145c-419a-b9dc-544bb5ad92c7                145   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 101                  200                   200   \n",
       "\n",
       "   faceAttributes.smile  faceAttributes.headPose.pitch  \\\n",
       "0                   0.0                           -9.3   \n",
       "\n",
       "   faceAttributes.headPose.roll  faceAttributes.headPose.yaw  \\\n",
       "0                          -2.6                         -3.4   \n",
       "\n",
       "  faceAttributes.gender  ...  faceAttributes.blur.value  \\\n",
       "0                  male  ...                       0.25   \n",
       "\n",
       "   faceAttributes.exposure.exposureLevel  faceAttributes.exposure.value  \\\n",
       "0                           goodExposure                           0.58   \n",
       "\n",
       "   faceAttributes.noise.noiseLevel faceAttributes.noise.value  \\\n",
       "0                              low                       0.22   \n",
       "\n",
       "   faceAttributes.makeup.eyeMakeup  faceAttributes.makeup.lipMakeup  \\\n",
       "0                            False                            False   \n",
       "\n",
       "   faceAttributes.hair.bald  faceAttributes.hair.invisible  \\\n",
       "0                      0.17                          False   \n",
       "\n",
       "                       faceAttributes.hair.hairColor  \n",
       "0  [{'color': 'black', 'confidence': 1.0}, {'colo...  \n",
       "\n",
       "[1 rows x 34 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "# from pandas.io.json import json_normalize\n",
    "df_face = pd.json_normalize(results)\n",
    "df_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "409"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "# 1、create  列表\n",
    "# faceListId\n",
    "faceListId = \"vicky_03\"\n",
    "create_facelists_url = \"https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "subscription_key = \"7584f022831e438bb7971ab31f094861\"\n",
    "assert subscription_key\n",
    "\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 1、create 创建一个装相片的列表\n",
    "data = {\n",
    "    \"name\": \"test_相册簿\",\n",
    "    \"userData\": \"pm_c的同学们\",#描述\n",
    "    \"recognitionModel\": \"recognition_03\",\n",
    "}\n",
    "# params = {\n",
    "#     # Request parameters    \n",
    "   \n",
    "#     \"faceListId\":\"who_you\"\n",
    "    \n",
    "# #     'detectionModel': 'detection_01',\n",
    "# }\n",
    "r_create = requests.put(create_facelists_url.format(faceListId),headers=headers,json=data)\n",
    "# 请求成功，返回空字符串\n",
    "r_create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"error\":{\"code\":\"FaceListExists\",\"message\":\"Face list \\'vicky_03\\' already exists.\"}}'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '1870a9c9-80d2-4f97-a9cb-a8f0c45e4b33',\n",
       "   'userData': '丘小峰'},\n",
       "  {'persistedFaceId': 'cdb8611b-3288-42e0-8365-9aa50776e919',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': 'e8dc1146-56cf-421b-8f92-bf54067791f8',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': 'fbf1b7a2-dc8a-463d-b507-aebe9a62ad13',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': 'db6b4022-3de9-4475-8063-a58f13c1326c',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '41106664-b023-4d4e-a265-4f37ce09f1dc',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': '37aa0c65-acdc-44d2-b788-4144f2ff2d10',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '285318e4-4248-4a13-b961-71f35378c3fc',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': 'f5335ad6-4297-4dc0-8658-8a8df4971e4c',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': '0b983d01-4087-450e-8607-4d79425f4f5a',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '3e159860-76f3-4d42-98b7-f94b05123cfb',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'e374de8a-f0a2-4f4a-b820-fcc6af36ac20',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': '75a8436a-103f-4b42-b371-38be81888e02',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': '6a916d78-07eb-4123-9644-4b5f4b1fbc50',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': 'aa93dd8f-d5c2-42f0-8cf2-2d597fd0b13b',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': 'fa60db76-1148-45ca-ae00-50cb8ec98e77',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'd48c2d06-167c-408b-bd11-50800a3f46a4',\n",
       "   'userData': '张铭睿'}],\n",
       " 'faceListId': 'vicky_03',\n",
       " 'name': 'test_相册簿',\n",
       " 'userData': 'pm_c的同学们'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Get facelist\n",
    "get_facelist_url = \"https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)#学生填写\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url = \"https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://huangjieqi.gitee.io/picture_storage/EdisonQXF.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"userData\":\"丘小峰\"\n",
    "}\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "429"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error': {'code': '429',\n",
       "  'message': 'Requests to the Face - Detect Operation under Face API - v1.0 have exceeded rate limit of your current Face F0 pricing tier. Please retry after 18 seconds. To increase your rate limit switch to a paid tier.'}}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://huangjieqi.gitee.io/picture_storage/hjq.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "findsimilars_url = \"https://apifacevicky-newmedia.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"3be61075-2e1c-4c8d-9ce9-15d1ad5b099f\",#取上方的faceID\n",
    "    \"faceListId\": \"vicky_03\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,#返回的最前面相似的面部数。 有效范围为 [1，1000]\n",
    "    \"mode\": \"matchFace\"#matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error': {'code': '429',\n",
       "  'message': 'Requests to the Face - Find Similar Operation under Face API - v1.0 have exceeded rate limit of your current Face F0 pricing tier. Please retry after 18 seconds. To increase your rate limit switch to a paid tier.'}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1870a9c9-80d2-4f97-a9cb-a8f0c45e4b33</td>\n",
       "      <td>丘小峰</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>cdb8611b-3288-42e0-8365-9aa50776e919</td>\n",
       "      <td>丘天惠</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>e8dc1146-56cf-421b-8f92-bf54067791f8</td>\n",
       "      <td>林嘉茵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fbf1b7a2-dc8a-463d-b507-aebe9a62ad13</td>\n",
       "      <td>汤玲萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>db6b4022-3de9-4475-8063-a58f13c1326c</td>\n",
       "      <td>曾雯燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>41106664-b023-4d4e-a265-4f37ce09f1dc</td>\n",
       "      <td>谢依希</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>37aa0c65-acdc-44d2-b788-4144f2ff2d10</td>\n",
       "      <td>杨悦聪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>285318e4-4248-4a13-b961-71f35378c3fc</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>f5335ad6-4297-4dc0-8658-8a8df4971e4c</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0b983d01-4087-450e-8607-4d79425f4f5a</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>3e159860-76f3-4d42-98b7-f94b05123cfb</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>e374de8a-f0a2-4f4a-b820-fcc6af36ac20</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>75a8436a-103f-4b42-b371-38be81888e02</td>\n",
       "      <td>刘宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>6a916d78-07eb-4123-9644-4b5f4b1fbc50</td>\n",
       "      <td>李婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>aa93dd8f-d5c2-42f0-8cf2-2d597fd0b13b</td>\n",
       "      <td>黄智毅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>fa60db76-1148-45ca-ae00-50cb8ec98e77</td>\n",
       "      <td>黄慧文</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>d48c2d06-167c-408b-bd11-50800a3f46a4</td>\n",
       "      <td>张铭睿</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   1870a9c9-80d2-4f97-a9cb-a8f0c45e4b33      丘小峰\n",
       "1   cdb8611b-3288-42e0-8365-9aa50776e919      丘天惠\n",
       "2   e8dc1146-56cf-421b-8f92-bf54067791f8      林嘉茵\n",
       "3   fbf1b7a2-dc8a-463d-b507-aebe9a62ad13      汤玲萍\n",
       "4   db6b4022-3de9-4475-8063-a58f13c1326c      曾雯燕\n",
       "5   41106664-b023-4d4e-a265-4f37ce09f1dc      谢依希\n",
       "6   37aa0c65-acdc-44d2-b788-4144f2ff2d10      杨悦聪\n",
       "7   285318e4-4248-4a13-b961-71f35378c3fc       周雨\n",
       "8   f5335ad6-4297-4dc0-8658-8a8df4971e4c      刘瑜鹏\n",
       "9   0b983d01-4087-450e-8607-4d79425f4f5a      陈嘉仪\n",
       "10  3e159860-76f3-4d42-98b7-f94b05123cfb      徐旖芊\n",
       "11  e374de8a-f0a2-4f4a-b820-fcc6af36ac20      刘心如\n",
       "12  75a8436a-103f-4b42-b371-38be81888e02       刘宇\n",
       "13  6a916d78-07eb-4123-9644-4b5f4b1fbc50       李婷\n",
       "14  aa93dd8f-d5c2-42f0-8cf2-2d597fd0b13b      黄智毅\n",
       "15  fa60db76-1148-45ca-ae00-50cb8ec98e77      黄慧文\n",
       "16  d48c2d06-167c-408b-bd11-50800a3f46a4      张铭睿"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#facelist里面的数据\n",
    "import pandas as pd\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3e159860-76f3-4d42-98b7-f94b05123cfb</td>\n",
       "      <td>0.29269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>6a916d78-07eb-4123-9644-4b5f4b1fbc50</td>\n",
       "      <td>0.20908</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>d48c2d06-167c-408b-bd11-50800a3f46a4</td>\n",
       "      <td>0.17849</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0b983d01-4087-450e-8607-4d79425f4f5a</td>\n",
       "      <td>0.16209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>e374de8a-f0a2-4f4a-b820-fcc6af36ac20</td>\n",
       "      <td>0.15023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>e8dc1146-56cf-421b-8f92-bf54067791f8</td>\n",
       "      <td>0.10034</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1870a9c9-80d2-4f97-a9cb-a8f0c45e4b33</td>\n",
       "      <td>0.09955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>aa93dd8f-d5c2-42f0-8cf2-2d597fd0b13b</td>\n",
       "      <td>0.09503</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>cdb8611b-3288-42e0-8365-9aa50776e919</td>\n",
       "      <td>0.09405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>fa60db76-1148-45ca-ae00-50cb8ec98e77</td>\n",
       "      <td>0.09298</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  3e159860-76f3-4d42-98b7-f94b05123cfb     0.29269\n",
       "1  6a916d78-07eb-4123-9644-4b5f4b1fbc50     0.20908\n",
       "2  d48c2d06-167c-408b-bd11-50800a3f46a4     0.17849\n",
       "3  0b983d01-4087-450e-8607-4d79425f4f5a     0.16209\n",
       "4  e374de8a-f0a2-4f4a-b820-fcc6af36ac20     0.15023\n",
       "5  e8dc1146-56cf-421b-8f92-bf54067791f8     0.10034\n",
       "6  1870a9c9-80d2-4f97-a9cb-a8f0c45e4b33     0.09955\n",
       "7  aa93dd8f-d5c2-42f0-8cf2-2d597fd0b13b     0.09503\n",
       "8  cdb8611b-3288-42e0-8365-9aa50776e919     0.09405\n",
       "9  fa60db76-1148-45ca-ae00-50cb8ec98e77     0.09298"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度的数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'persistedFaceId'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-56-c31da20b8105>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#合并在一起，得出班级能谁最像你\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmerge\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfaceListID_df\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbdf\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mhow\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'inner'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mon\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'persistedFaceId'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mby\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"confidence\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mascending\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Anaconda3\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36mmerge\u001b[1;34m(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)\u001b[0m\n\u001b[0;32m     71\u001b[0m     \u001b[0mvalidate\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     72\u001b[0m ) -> \"DataFrame\":\n\u001b[1;32m---> 73\u001b[1;33m     op = _MergeOperation(\n\u001b[0m\u001b[0;32m     74\u001b[0m         \u001b[0mleft\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     75\u001b[0m         \u001b[0mright\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, left, right, how, on, left_on, right_on, axis, left_index, right_index, sort, suffixes, copy, indicator, validate)\u001b[0m\n\u001b[0;32m    625\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mright_join_keys\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    626\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin_names\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 627\u001b[1;33m         ) = self._get_merge_keys()\n\u001b[0m\u001b[0;32m    628\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    629\u001b[0m         \u001b[1;31m# validate the merge keys dtypes. We may need to coerce\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\anaconda3\\lib\\site-packages\\pandas\\core\\reshape\\merge.py\u001b[0m in \u001b[0;36m_get_merge_keys\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    994\u001b[0m                         \u001b[0mright_keys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    995\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[0mlk\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 996\u001b[1;33m                         \u001b[0mleft_keys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mleft\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_label_or_level_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    997\u001b[0m                         \u001b[0mjoin_names\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    998\u001b[0m                     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Anaconda3\\anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m_get_label_or_level_values\u001b[1;34m(self, key, axis)\u001b[0m\n\u001b[0;32m   1690\u001b[0m             \u001b[0mvalues\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxes\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_level_values\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1691\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1692\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1693\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1694\u001b[0m         \u001b[1;31m# Check for duplicates\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'persistedFaceId'"
     ]
    }
   ],
   "source": [
    "#合并在一起，得出班级能谁最像你\n",
    "pd.merge(faceListID_df, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
